/**
  ******************************************************************************
  * @file    Bsp/glcd.h 
  * @author  zlf
  * @version V1.0.0
  * @date    2016-04-06
  * @brief   Header for glcd module for ks0108 or sed1520
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/

#ifndef __GLCD_H
#define __GLCD_H

/* Includes ------------------------------------------------------------------*/
	#include "main.h"

/* Define to c++ expension ---------------------------------------------------*/
#ifdef __cplusplus
extern "C" {
#endif

/* Exported constants --------------------------------------------------------*/
#define GLCD_CTRL_GPIO1  	( GPIOD )
#define GLCD_CTRL_RS 		( GPIO_Pin_13 )
#define GLCD_CTRL_RW  		( GPIO_Pin_14 )
#define GLCD_CTRL_E 		( GPIO_Pin_15 )

#define GLCD_CTRL_GPIO2  	( GPIOD )
#define GLCD_CTRL_CS0 		( GPIO_Pin_9 )
#define GLCD_CTRL_CS1 		( GPIO_Pin_10 )
#define GLCD_CTRL_RESET 	( GPIO_Pin_12 )

#define GLCD_DATA_GPIO 		( GPIOD )
#define GLCD_DATA_PIN 		( 0x00ff ) //0xff00
#define GLCD_DATA_MASK 		( GLCD_DATA_PIN )

//the port action
#define SET_LCD_DATA 		( GPIO_SetBits(GLCD_CTRL_GPIO1, GLCD_CTRL_RS) )
#define SET_LCD_CMD 		( GPIO_ResetBits(GLCD_CTRL_GPIO1, GLCD_CTRL_RS) )
#define SET_LCD_READ 		( GPIO_SetBits(GLCD_CTRL_GPIO1, GLCD_CTRL_RW) )
#define SET_LCD_WRITE 		( GPIO_ResetBits(GLCD_CTRL_GPIO1, GLCD_CTRL_RW) )
#define SET_LCD_E           ( GPIO_SetBits(GLCD_CTRL_GPIO1, GLCD_CTRL_E) )
#define CLR_LCD_E           ( GPIO_ResetBits(GLCD_CTRL_GPIO1, GLCD_CTRL_E) )

#define SET_LCD_CS0 		( GPIO_SetBits(GLCD_CTRL_GPIO2, GLCD_CTRL_CS0) )
#define CLR_LCD_CS0 		( GPIO_ResetBits(GLCD_CTRL_GPIO2, GLCD_CTRL_CS0) )
#define SET_LCD_CS1 		( GPIO_SetBits(GLCD_CTRL_GPIO2, GLCD_CTRL_CS1) )
#define CLR_LCD_CS1 		( GPIO_ResetBits(GLCD_CTRL_GPIO2, GLCD_CTRL_CS1) )
#define SET_LCD_RESET 		( GPIO_SetBits(GLCD_CTRL_GPIO2, GLCD_CTRL_RESET) )
#define CLR_LCD_RESET 		( GPIO_ResetBits(GLCD_CTRL_GPIO2, GLCD_CTRL_RESET) )

//#define LCD_DATA_READ		( GPIO_ReadInputData(GLCD_DATA_GPIO) >> 8 )
//#define LCD_DATA_WRITE(dat)	GPIO_Write(GLCD_DATA_GPIO,GPIO_ReadOutputData(GLCD_DATA_GPIO)&0x00ff|(dat<<8))
#define LCD_DATA_READ		( GPIO_ReadInputData(GLCD_DATA_GPIO) & 0x00ff )
#define LCD_DATA_WRITE(dat)	GPIO_Write(GLCD_DATA_GPIO,GPIO_ReadOutputData(GLCD_DATA_GPIO)& 0xff00 | dat)

//Lcd Pixels
#define GLCD_XPIXELS 			( 128 )
#define GLCD_YPIXELS 			( 64 )
	
#define GLCD_CONTROLLER_PIXELS 	( 64 )
#define GLCD_MAX_PAGE 			( 8 )

//sed1520 command set
#define GLCD_ON_CTRL 			( 0x3E ) //1010111X: lcd on/off control
#define GLCD_ON_DISPLAY 		( 0x01 ) //Turn display on

#define GLCD_START_LINE 		( 0xC0 ) //11XXXXXX: set lcd start line

#define GLCD_SET_PAGE 			( 0xB8 ) //10111XXX: set lcd page (X) address
#define GLCD_SET_COLUMN 		( 0x40 ) //01YYYYYY: set lcd Y address
#define GLCD_COLUMN_MASK 		( 0x3F )          

#define GLCD_STATUS_BUSY 		( 0x80 ) //LCD is BUSY
#define GLCD_STATUS_ONOFF 		( 0x20 ) //LCD is on
#define GLCD_STATUS_RESET 		( 0x10 ) //LCD is reset

#define GLCD_LEFT_CONTROLLER 		0
#define GLCD_RIGHT_CONTROLLER 		1

#define E_CYCLE_TIME 				8
#define RESET_CYCLE_TIME 			50

/* Exported types ------------------------------------------------------------*/
typedef struct
{
	u8 LcdXAddress;
	u8 LcdYAddress;
}GlcdPositionStructType;

/* Exported functions --------------------------------------------------------*/
extern void Glcd_Init_HW(void);
extern void Glcd_Busy_Wait( u8 controller );
extern void Glcd_Control_Write(u8 controller, u8 data );
extern u8   Glcd_Control_Read(u8 controller);
extern void Glcd_Data_Write(u8 data);
extern void Glcd_Set_X_Address(u8 xAddr);
extern void Glcd_Set_Y_Address(u8 yAddr);
extern void Glcd_Set_XY_Address(u8 xAddr, u8 yAddr);
extern void Glcd_Init(void);
extern u8   Glcd_Data_Read(void);
extern void Glcd_Clear_Screen(void);
extern void Glcd_Clear_Downhalf_Screen(void);
extern void Glcd_DeInit(void);

/* Exported varables --------------------------------------------------------*/
extern GlcdPositionStructType Glcd_Position_Cur;

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __GLCD_H */


